<?php

use yii\helpers\Html;
use yii\helpers\ArrayHelper;
use yii\widgets\ActiveForm;
use common\models\Region;

/* @var $this yii\web\View */
/* @var $model common\models\Region */
/* @var $form yii\widgets\ActiveForm */

$routes = [];
$ids = $model->isNewRecord ? [0] : array_merge($model->route,[$model->id]);

foreach ($ids as $id) {
    $child = Region::find()->where(['parent_id' => $id])->all();
    if ($child) {
        $ret = ArrayHelper::map($child, 'id', 'name');
        $routes[] = $ret;
    }
}

?>

<div class="region-form">
    <?php foreach ($routes as $item): ?>
        <select name="Region[parent_id][]" class="parent" >
            <option value="0">请选择</option>
            <?php foreach ($item as $id => $name): ?>
                <option value="<?= $id ?>" <?php if (in_array($id, $ids)): ?>selected<?php endif; ?>><?= $name ?></option>
            <?php endforeach; ?>
        </select>
    <?php endforeach; ?>
</div>

<script>
    $('.parent').bind('change', function () {
        var id = $(this).val();
        if(id==0){
            //return false;
        };
        var url = '<?= yii\helpers\Url::to(['get-parent']) ?>';
        $.get(url, {id: id}, function (ret) {
            $('#parentList').html(ret);
        });
    });

</script>